﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DualityEngine.Business.GameModel
{
    public class Planet : GameModel
    {
        private float modelrotation = 0.0f;
        private bool isrotating = false;

        public Planet(Vector3 position, Model model, bool isrotating)
            : base(position, model)
        {
            this.isrotating = isrotating;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            modelrotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(0.01f);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    if(isrotating)
                        effect.Parameters["World"].SetValue(transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(modelrotation) * Matrix.CreateTranslation(this.position));
                    else
                        effect.Parameters["World"].SetValue(transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(this.position));
                    effect.Parameters["View"].SetValue(CameraManager.Instance.ActiveCamera.viewmatrix);
                    effect.Parameters["Projection"].SetValue(CameraManager.Instance.ActiveCamera.projectionmatrix);
                }
                mesh.Draw();
            }
        }
    }
}
